//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package ciicos.visualdata.controller;

import ciicos.base.ActionResult;
import ciicos.base.controller.SuperController;
import ciicos.util.JsonUtil;
import ciicos.visualdata.entity.VisualMapEntity;
import ciicos.visualdata.model.VisualPageVO;
import ciicos.visualdata.model.VisualPagination;
import ciicos.visualdata.model.visualmap.VisualMapCrForm;
import ciicos.visualdata.model.visualmap.VisualMapInfoVO;
import ciicos.visualdata.model.visualmap.VisualMapListVO;
import ciicos.visualdata.model.visualmap.VisualMapUpForm;
import ciicos.visualdata.service.VisualMapService;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Tag(
        name = "大屏地图",
        description = "map"
)
@RequestMapping({"/api/blade-visual/map"})
public class VisualMapController extends SuperController<VisualMapService, VisualMapEntity> {
    @Autowired
    private VisualMapService mapService;

    @Operation(
            summary = "分页"
    )
    @GetMapping({"/list"})
    public ActionResult<VisualPageVO<VisualMapListVO>> list(VisualPagination pagination) {
        List<VisualMapEntity> data = this.mapService.getListWithColnums(pagination, new SFunction[]{VisualMapEntity::getId, VisualMapEntity::getName});
        List<VisualMapListVO> list = JsonUtil.getJsonToList(data, VisualMapListVO.class);
        VisualPageVO paginationVO = (VisualPageVO)JsonUtil.getJsonToBean(pagination, VisualPageVO.class);
        paginationVO.setRecords(list);
        return ActionResult.success(paginationVO);
    }

    @Operation(
            summary = "详情"
    )
    @GetMapping({"/detail"})
    public ActionResult<VisualMapInfoVO> info(String id) {
        VisualMapEntity entity = this.mapService.getInfo(id);
        VisualMapInfoVO vo = (VisualMapInfoVO)JsonUtil.getJsonToBean(entity, VisualMapInfoVO.class);
        return ActionResult.success(vo);
    }

    @Operation(
            summary = "新增"
    )
    @PostMapping({"/save"})
    @Parameters({@Parameter(
            name = "mapCrForm",
            description = "地图模型",
            required = true
    )})
    @SaCheckPermission({"onlineDev.dataScreen"})
    public ActionResult create(@RequestBody VisualMapCrForm mapCrForm) {
        VisualMapEntity entity = (VisualMapEntity)JsonUtil.getJsonToBean(mapCrForm, VisualMapEntity.class);
        this.mapService.create(entity);
        return ActionResult.success("新建成功");
    }

    @Operation(
            summary = "修改"
    )
    @PostMapping({"/update"})
    @Parameters({@Parameter(
            name = "mapUpForm",
            description = "地图模型",
            required = true
    )})
    @SaCheckPermission({"onlineDev.dataScreen"})
    public ActionResult update(@RequestBody VisualMapUpForm mapUpForm) {
        VisualMapEntity entity = (VisualMapEntity)JsonUtil.getJsonToBean(mapUpForm, VisualMapEntity.class);
        boolean flag = this.mapService.update(mapUpForm.getId(), entity);
        return !flag ? ActionResult.fail("更新失败，数据不存在") : ActionResult.success("更新成功");
    }

    @Operation(
            summary = "删除"
    )
    @PostMapping({"/remove"})
    @Parameters({@Parameter(
            name = "ids",
            description = "主键",
            required = true
    )})
    @SaCheckPermission({"onlineDev.dataScreen"})
    public ActionResult delete(@RequestParam("ids") String ids) {
        VisualMapEntity entity = this.mapService.getInfo(ids);
        if (entity != null) {
            this.mapService.delete(entity);
            return ActionResult.success("删除成功");
        } else {
            return ActionResult.fail("删除失败，数据不存在");
        }
    }

    @Operation(
            summary = "数据详情"
    )
    @GetMapping({"/data"})
    @Parameters({@Parameter(
            name = "id",
            description = "主键",
            required = true
    )})
    public Map<String, Object> dataInfo(@RequestParam("id") String id) {
        VisualMapEntity entity = this.mapService.getInfo(id);
        Map<String, Object> data = JsonUtil.stringToMap(entity.getData());
        return data;
    }
}
